<?php

namespace Addons\Upload\Controller;
class IndexController extends \Addon
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 多文件上传
     * @return
     */
    public function index($param)
    {
        $this->_static = "/Addons/Upload/_static";
        $this->param = $param;
        $this->path = $param['path'];
        $this->exts = $param['exts'];
        if ($GLOBALS['uploadAddonHasLoad'] != 1) {
            $GLOBALS['uploadAddonHasLoad'] = 1;
            $this->display();
        }
    }

    public function showUpload($param)
    {
        $param['jsonDecodeValue'] = $param['value'] ? json_decode($param['value'], true) : array();
        $this->param = $param;
        $GLOBALS['show_modal_int'] = $GLOBALS['show_modal_int'] ? $GLOBALS['show_modal_int'] : 0;
        $GLOBALS['show_modal_int']++;
        $this->show_modal_int = $GLOBALS['show_modal_int'];
        if ($param['view'] == "web") {
            $this->display("show");
        } else {
            $this->display("show_wap_self");
        }
    }

    public function get_upload_ext($extension = "image")
    {
        $ext_arr = array(
            'image' => array(
                'gif',
                'jpg',
                'jpeg',
                'png',
                'bmp'
            ),
            'video' => array(
                'swf',
                'flv',
                'mp3',
                'wav',
                'wma',
                'wmv',
                'mid',
                'avi',
                'mpg',
                'mp4',
                'asf',
                'rm',
                'rmvb'
            ),
            'file' => array(
                'doc',
                'docx',
                'xls',
                'xlsx',
                'ppt',
                'htm',
                'html',
                'txt',
                'zip',
                'rar',
                'gz',
                'bz2'
            ),
        );
        $p = explode(",", $extension);
        $ext_arr_array = array();
        foreach ($p as $one) {
            $ext_arr_array = array_merge($ext_arr_array, (array)$ext_arr[$one]);
        }
        return $ext_arr_array;
    }

    public function upload($params)
    {
        $type = $params['type'];
        $exts = I('exts', "image");
        $act = $_GET['act'];
        $param = array(
            'savePath' => I('path', "default"), //文件保存目录，相对UploadPath
            'maxSize' => 20000000000, //文件大小限制200M,
            'exts' => $this->get_upload_ext($exts), //上传图片

        );
        set_time_limit(0);
        //header("Content-type: text/html; charset=utf-8");
        $info = $this->_upload($param);
        if (!is_array($info)) {
            $return = $this->error($info->getError());
        } else {
            $info = array_shift($info);
            $return = array(
                'error' => 0,
                'data' => array(
                    'new_file_name' => $info['savename'],
                    'save_path' => $info['savepath'],
                    'name' => $info['name'],
                    'file_url' => ImgDomain . "/" . $info['savepath'] . $info['savename'],
                    'file_path' => $info['savepath'] . $info['savename'],
                )
            );
            if ($act) {
                //dump("upload_attach_complete_".$act);exit();
                hook("upload_attach_complete_" . $act, $return); //附件上传完成勾子

            }
            hook("upload_attach_complete", $return); //附件上传完成勾子
        }
        if ($type == "return") {
            return $return;
        } else {
            exit(json_encode($return));
        }
    }

    /*******错误**/
    public function error($msg)
    {
        $r = array(
            'error' => 1,
            'msg' => $msg
        );
        if (I('act')) {
            hook("upload_attach_error_" . $act, $return); //附件上传完成勾子

        }
        return $r;
        //  exit(json_encode($r));
    }

    public function upload_video()
    {
		 set_time_limit(0);
        $param = array(
            'fileContent' => $_FILES['imgFile']['tmp_name'],
            'fileName' => $_FILES['imgFile']['name'],
        );
		if(!$_FILES['imgFile']){
		   exit(json_encode($this->error(" 请先上传文件")));
	
		}
		
        $f = explode(".", $_FILES['imgFile']['name']);
        $ext = $f[count($f) - 1];
        $exts = $this->get_upload_ext("video");
        if (!in_array($ext, $exts)) {
            exit(json_encode($this->error("请上传格式：" . implode(",", $exts))));
        }
        if ($_FILES['imgFile']['error']>0) {
           exit(json_encode($this->error(" 文件过大，请设置php.ini,错误码：".$_FILES['imgFile']['error'])));
        } else {
            $videoClass = new \Common\Org\Video\Video();
            $data = $videoClass->upload($param);
            $info = array(
                'error' => 0,
                'new_file_name' => $data['fileName'],
                'file_url' => $data['location'],
                'data' => $data['data'],
            );
            exit(json_encode($info));
        }
    }

    public function progress()
    {
        $fileName = trim(I('fileName'));
        $param = array(
            'fileName' => $fileName,
            'session_id' => session_id(),
        );
        $key = md5(json_encode($param));
        $progress = S($key);
        if (is_numeric($progress)) {
            $progress = intval(S($key) * 100); //上传进度结束
            echo $progress;
        } else {
            echo 'default';
        }
    }

    /******上传进度***/
    public function progress_video()
    {
        $fileName = trim(I('fileName'));
        $param = array(
            'fileName' => $fileName,
        );
        $videoClass = new \Common\Org\Video\Video();
        $data = $videoClass->progress($param);
        echo $data;
    }
}
